Welcome back. Since CSV is just a special format,
you can read it like any other file.
In fact, we've been reading a file that was in CSV format already, the Olympics file.
It didn't follow the conventions of using.CSV as the ending for the file name,
but the actual contents were in CSV format.
The advantage when we know that something is in CSV format is that it's easy to parse it.
We can just chop up each line into
its individual components by looking for where the commas are.
For example, take a look at this code.
On lines one through four,
I'm just reminding you of what the contents of this file are.
So, you can see the first line is a header,
and then I'm printing out more lines up to line six.
Each line has somebody's name and the other values they're separated by commas.
I want to show you how easy it is to process
these contents because we can just use the.split method looking for commas.
So, on line six of our code,
we're looking just at the header line,
that's this one, and we're saying,
well, first of all,
get rid of the newline character at the end of that line.
Then, split and split wherever you see a comma.
So, where there's a comma,
we're going to chop up the text and we're going to get,
actually, we can see the output here because on line eight, we're printing it out.
So, we have name is the first value,
it's the characters that occur before the first comma,
and our next value is the string sex,
and then we have the string age.
All of these are coming from this one line of text,
but we've chopped it up to make a list,
and that's what the.split command does for us.
We're doing something pretty similar with the rest of the lines.
We're looping through all of them and for each of them,
we're chopping it up wherever you find a comma on the line.
Now, once we take a line like A Lamusi,M,23,China,Judo,NA,
and we split it up into a list,
we can now use indexing.
I can ask for the value that's in index five,
the sixth element from that line,
and we can check, is its value NA?
Well, sure enough its value is NA,
and we do one thing if it's NA,
we do something else if it's not.
In this case, if it's not NA,
then we're going to print something out.
So, we're going to print out something only for the people who actually won a medal.
We're going to skip the people who didn't win a medal.
You can see that output that comes down here.
So, only people who won a gold,
a bronze or a silver will show up in our output.
We're choosing here to not print the whole line,
we're printing three elements from that line,
the three curly braces,
and we're printing out vowel square bracket zero, that's the name.
It's the string that comes before the first comma,
and we're getting the thing from the position four and from position five.
So, that's the name and the event and the medal that they won.
Now, note that we have to split on commas.
I think before when we've seen the split command,
we've tend to just split without specifying the value.
When you don't specify a value,
it splits wherever it finds any whitespace,
a space or a tab or a new line.
If we do that, we're going to see that we get something different.
We're not going to get this nice list here,
we're going to get a different list.
Sure enough, what we get is a list with only one element in it.
It's one big string,
all of its commas and everything,
it hasn't split it up into seven different elements or six different elements,
it's just giving us one big thing.
The reason is, it was looking for a whitespace and in this whole string,
there are no spaces,
no tabs, no carriage returns.
So, we just get one item.
If we had split on something else,
let's say the letter E,
it would split wherever there was an E,
and we'll get some weird thing.
I have to split on the character E,
not the variable name E. We're just waiting for it to finish.
Sure enough, our first value is Nam and then there was an E,
and then after that there is a comma and a capital S,
and then there was another E and so on.
So, split will split on whatever you tell it to split on.
In our case, we want to split on commas because the comma
separated value format says commas are the things that separate the values.
By the way, there is another more advanced version of
the CSV format that separates with commas but encloses all of the values in quotes.
Let's see what that looks like.
Here you can see in this file format,
you can see that some events have commas in them, while others don't.
For example, we have Speed Skating, 1500 meters.
Whereas, for Tug-Of-War or Basketball,
there's no comma in it.
That's going to make it harder to parse because when there's a comma,
we don't know whether it's part of a value or separating values.
If we were to just split on comma like we did before,
vals equals row.split and comma.
Then we said vals square bracket five,
like we did before,
the fifth element or the index five,
the sixth element of this row will be NA,
but the fifth element of this row will be the 1500 meters.
So, life gets more complicated when we want to parse
this more advanced comma separated values format
that also has quotes around each of the values.
It actually is still possible to unambiguously chop up the lines,
but that's a harder programming challenge.
I don't recommend trying it yourself.
Instead, when you encounter something in this format,
you would use Python CSV module to parse the lines for you.
We're not going to learn that module right now.
I've found that it's good for students to learn how to parse simple CSVs using
the.split method at this point for understanding what's really going on.
Later, you can learn to use the CSV module for harder formats.
In summary, when we have a simple CSV format
with commas separating and no quotes around all the values, parsing is easy.
You just read in the file, align it at a time,
and you use the split method specifying comma as the thing to split on.
That gives you a list of the individual values or
the individual field names on the header line. I'll see you next time.